var express = require("express");
var router = express.Router();
var menuinfoService = require("../../service/auth/menuinfoService");
//菜单列表
router.get(
  "/list",
  async (req, res, next) => {
    let list = await menuinfoService.query(req.query);
    res.json({ code: 1, data: list });
  },
  (err) => {
    res.json({ code: 0, msg: err });
  }
);
//新增菜单
router.post(
  "/add",
  async (req, res, next) => {
    menuinfoService.add(req.body).then(() => {
      res.json({ code: 1, msg: "新增成功" });
    });
  },
  (err) => {
    res.json({ code: 0, msg: err });
  }
);
//更新菜单
router.post(
  "/update",
  async (req, res, next) => {
    let { _id } = req.body;
    if (_id == "" || !_id) return res.json({ code: 0, msg: "_id不存在" });
    menuinfoService.update(req.body).then(() => {
      res.json({ code: 1, msg: "编辑成功" });
    });
  },
  (err) => {
    res.json({ code: 0, msg: err });
  }
);
//删除菜单
router.post(
  "/remove",
  async (req, res, next) => {
    let { _id } = req.body;
    if (_id == "" || !_id) return res.json({ code: 0, msg: "_id不存在" });
    menuinfoService.delete({ _id: _id }).then(() => {
      res.json({ code: 1, msg: "删除成功" });
    });
  },
  (err) => {
    res.json({ code: 0, msg: err });
  }
);
//查询
router.get(
    "/model",
    async (req, res, next) => {
      let { _id } = req.query;
      let list = await menuinfoService.query({ _id });
      res.json({ code: 1, data: list.length > 0 ? list[0] : {} });
    },
    (err) => {
      res.json({ code: 0, msg: err });
    }
  );
module.exports = router;
